package org.artifact.core.plugin.i18n;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.artifact.core.lang.IPlugin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * i18n插件
 */
public class I18NPlugin implements IPlugin {

	static final Log log = LogFactory.get(I18NPlugin.class);

	/** i18n文件夹 */
	private String i18nDir;
	/** i18n文件前缀 */
	private String fileNamePrefix;
	/** i18n文件后缀 */
	private String fileNameSuffix;
	
	private final Map<String, I18nRes> resourceBundleMap = new HashMap<String, I18nRes>();

	public I18NPlugin(String i18nDir, String fileNamePrefix, String fileNameSuffix) {
		super();
		this.i18nDir = i18nDir;
		this.fileNamePrefix = fileNamePrefix;
		this.fileNameSuffix = fileNameSuffix;
	}
	
	public String get(String language, String key){
		I18nRes res = resourceBundleMap.get(language);
		if (res == null) {
			return key;
		}
		return res.get(key);
	}
	
	public String format(String language, String key,String...arguments){
		I18nRes res = resourceBundleMap.get(language);
		return res.format(key, arguments);
	}

	
	@Override
	public boolean start() {
		// 筛选国际化文件
		List<File> files = FileUtil.loopFiles(i18nDir, (file)->{
			return StrUtil.startWith(file.getName(), fileNamePrefix);
		});
		
		
		for (File file : files) {
			String language = StrUtil.subBetween(file.getName(), fileNamePrefix, fileNameSuffix);
			I18nRes res = resourceBundleMap.get(language);
			if (res == null) {
				res = new I18nRes(file);
				res.load();
				resourceBundleMap.put(language, res);
				log.debug("加载国际化资源文件：" + file.getName() + " 条数:" + res.getCount());
			}
		}
		return true;
	}

	@Override
	public boolean stop() {
		resourceBundleMap.clear();
		return true;
	}

	class I18nRes{
		private File file;
		private Map<String, String> contents = new HashMap<>();
		
		public I18nRes(File file) {
			super();
			this.file = file;
		}

		public void load() {
			Properties properties = null;
			FileInputStream inputStream = null;
			try {
				properties = new Properties();
				inputStream =  new FileInputStream(file);
				properties.load(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
			Enumeration<Object> keys = properties.keys();
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				String value = properties.getProperty(key);
				contents.put(key, value);
			}
		}
		
		public String get(String key) {
			return contents.getOrDefault(key, key);
		}
		
		public String format(String key, Object... arguments) {
			return MessageFormat.format(get(key), arguments);
		}
		
		public int getCount(){
			return contents.size();
		}
	}
}
